﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class SysModuleAction
    {
        public int Add(Chain.Model.SysModuleAction model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into SysModuleAction(");
            builder.Append("ActionCaption,ActionControl,ActionRemark,ActionModuleID)");
            builder.Append(" values (");
            builder.Append("@ActionCaption,@ActionControl,@ActionRemark,@ActionModuleID)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ActionCaption", SqlDbType.NVarChar, 50), new SqlParameter("@ActionControl", SqlDbType.VarChar, 200), new SqlParameter("@ActionRemark", SqlDbType.NVarChar, 200), new SqlParameter("@ActionModuleID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.ActionCaption;
            cmdParms[1].Value = model.ActionControl;
            cmdParms[2].Value = model.ActionRemark;
            cmdParms[3].Value = model.ActionModuleID;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public Chain.Model.SysModuleAction DataRowToModel(DataRow row)
        {
            Chain.Model.SysModuleAction action = new Chain.Model.SysModuleAction();
            if (row != null)
            {
                if ((row["ActionID"] != null) && (row["ActionID"].ToString() != ""))
                {
                    action.ActionID = int.Parse(row["ActionID"].ToString());
                }
                if (row["ActionCaption"] != null)
                {
                    action.ActionCaption = row["ActionCaption"].ToString();
                }
                if (row["ActionControl"] != null)
                {
                    action.ActionControl = row["ActionControl"].ToString();
                }
                if (row["ActionRemark"] != null)
                {
                    action.ActionRemark = row["ActionRemark"].ToString();
                }
                if ((row["ActionModuleID"] != null) && (row["ActionModuleID"].ToString() != ""))
                {
                    action.ActionModuleID = new int?(int.Parse(row["ActionModuleID"].ToString()));
                }
            }
            return action;
        }

        public bool Delete(int ActionID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysModuleAction ");
            builder.Append(" where ActionID=@ActionID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ActionID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ActionID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string ActionIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysModuleAction ");
            builder.Append(" where ActionID in (" + ActionIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int ActionID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from SysModuleAction");
            builder.Append(" where ActionID=@ActionID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ActionID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ActionID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetGroupMenuAction(int PageID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("Select ActionID,ActionCaption,ActionControl,ActionRemark,ActionModuleID ");
            builder.Append(" FROM SysModuleAction ");
            if (PageID > 0)
            {
                builder.Append(" where ActionModuleID=" + PageID);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetGroupMenuActionNotPage(int PageID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("Select ActionID,ActionCaption,ActionControl,ActionRemark,ActionModuleID ");
            builder.Append(" FROM SysModuleAction ");
            if (PageID > 0)
            {
                builder.Append(" where ActionModuleID=" + PageID + " and ActionControl!='page'");
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ActionID,ActionCaption,ActionControl,ActionRemark,ActionModuleID ");
            builder.Append(" FROM SysModuleAction ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" ActionID,ActionCaption,ActionControl,ActionRemark,ActionModuleID ");
            builder.Append(" FROM SysModuleAction ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.ActionID desc");
            }
            builder.Append(")AS Row, T.*  from SysModuleAction T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ActionID", "SysModuleAction");
        }

        public Chain.Model.SysModuleAction GetModel(int ActionID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 ActionID,ActionCaption,ActionControl,ActionRemark,ActionModuleID from SysModuleAction ");
            builder.Append(" where ActionID=@ActionID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ActionID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ActionID;
            new Chain.Model.SysModuleAction();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                return this.DataRowToModel(set.Tables[0].Rows[0]);
            }
            return null;
        }

        public Chain.Model.SysModuleAction GetModelByModuleIDAndControl(int actionModuleID, string actionControl)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from SysModuleAction where ActionModuleID=@ActionModuleID and ActionControl=@ActionControl");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ActionModuleID", SqlDbType.Int, 4), new SqlParameter("@ActionControl", SqlDbType.VarChar, 200) };
            cmdParms[0].Value = actionModuleID;
            cmdParms[1].Value = actionControl;
            DataTable table = DbHelperSQL.Query(builder.ToString(), cmdParms).Tables[0];
            if (table.Rows.Count > 0)
            {
                return this.DataRowToModel(table.Rows[0]);
            }
            return null;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM SysModuleAction ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.SysModuleAction model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update SysModuleAction set ");
            builder.Append("ActionCaption=@ActionCaption,");
            builder.Append("ActionControl=@ActionControl,");
            builder.Append("ActionRemark=@ActionRemark,");
            builder.Append("ActionModuleID=@ActionModuleID");
            builder.Append(" where ActionID=@ActionID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ActionCaption", SqlDbType.NVarChar, 50), new SqlParameter("@ActionControl", SqlDbType.VarChar, 200), new SqlParameter("@ActionRemark", SqlDbType.NVarChar, 200), new SqlParameter("@ActionModuleID", SqlDbType.Int, 4), new SqlParameter("@ActionID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.ActionCaption;
            cmdParms[1].Value = model.ActionControl;
            cmdParms[2].Value = model.ActionRemark;
            cmdParms[3].Value = model.ActionModuleID;
            cmdParms[4].Value = model.ActionID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

